
#include "NadineFramework0316/NadineFramework/NadineLibrary/Nadine09.h"
#include "NDMenuDialog.h"

namespace Nadine
{
	//---------------------------------------------------------------------
	void NDMenuDialog::animateButton(float fElapsedTime)
	{
		float aniTime =fElapsedTime;
		if (aniTime >0.3f)
			aniTime =0.3f;

		//normal mode buttons
		for (int i=0;i<this->m_myDialog.myNormalButtons.GetSize();++i)
		{
			//position
			if (NDVec2Length(&(this->m_myDialog.myNormalButtons[i]->vTargetPosition -this->m_myDialog.myNormalButtons[i]->vCurrentPosition)) >0.5f)
			{
				this->m_myDialog.myNormalButtons[i]->vCurrentPosition +=(this->m_myDialog.myNormalButtons[i]->vTargetPosition -this->m_myDialog.myNormalButtons[i]->vCurrentPosition)*0.5f*aniTime*15.0f;
			}else
				this->m_myDialog.myNormalButtons[i]->vCurrentPosition =this->m_myDialog.myNormalButtons[i]->vTargetPosition;


			//mouse over blending
			if (fabs(this->m_myDialog.myNormalButtons[i]->fTargMouseOverAlpha -this->m_myDialog.myNormalButtons[i]->fCurrMouseOverAlpha) >0.05f)
			{
				this->m_myDialog.myNormalButtons[i]->fCurrMouseOverAlpha +=(this->m_myDialog.myNormalButtons[i]->fTargMouseOverAlpha -this->m_myDialog.myNormalButtons[i]->fCurrMouseOverAlpha)*0.5f*aniTime*15.0f;
			}else
				this->m_myDialog.myNormalButtons[i]->fCurrMouseOverAlpha =this->m_myDialog.myNormalButtons[i]->fTargMouseOverAlpha;

			if (fabs(this->m_myDialog.myNormalButtons[i]->fTargNormalAlpha -this->m_myDialog.myNormalButtons[i]->fCurrNormalAlpha) >0.05f)
			{
				this->m_myDialog.myNormalButtons[i]->fCurrNormalAlpha +=(this->m_myDialog.myNormalButtons[i]->fTargNormalAlpha -this->m_myDialog.myNormalButtons[i]->fCurrNormalAlpha)*0.5f*aniTime*15.0f;
			}else
				this->m_myDialog.myNormalButtons[i]->fCurrNormalAlpha =this->m_myDialog.myNormalButtons[i]->fTargNormalAlpha;

		}

		//ar mode buttons
		for (int i=0;i<this->m_myDialog.myARButtons.GetSize();++i)
		{
			//position
			if (NDVec2Length(&(this->m_myDialog.myARButtons[i]->vTargetPosition -this->m_myDialog.myARButtons[i]->vCurrentPosition)) >0.5f)
			{
				this->m_myDialog.myARButtons[i]->vCurrentPosition +=(this->m_myDialog.myARButtons[i]->vTargetPosition -this->m_myDialog.myARButtons[i]->vCurrentPosition)*0.5f*aniTime*15.0f;
			}else
				this->m_myDialog.myARButtons[i]->vCurrentPosition =this->m_myDialog.myARButtons[i]->vTargetPosition;


			//mouse over blending
			if (fabs(this->m_myDialog.myARButtons[i]->fTargMouseOverAlpha -this->m_myDialog.myARButtons[i]->fCurrMouseOverAlpha) >0.05f)
			{
				this->m_myDialog.myARButtons[i]->fCurrMouseOverAlpha +=(this->m_myDialog.myARButtons[i]->fTargMouseOverAlpha -this->m_myDialog.myARButtons[i]->fCurrMouseOverAlpha)*0.5f*aniTime*15.0f;
			}else
				this->m_myDialog.myARButtons[i]->fCurrMouseOverAlpha =this->m_myDialog.myARButtons[i]->fTargMouseOverAlpha;

			if (fabs(this->m_myDialog.myARButtons[i]->fTargNormalAlpha -this->m_myDialog.myARButtons[i]->fCurrNormalAlpha) >0.05f)
			{
				this->m_myDialog.myARButtons[i]->fCurrNormalAlpha +=(this->m_myDialog.myARButtons[i]->fTargNormalAlpha -this->m_myDialog.myARButtons[i]->fCurrNormalAlpha)*0.5f*aniTime*15.0f;
			}else
				this->m_myDialog.myARButtons[i]->fCurrNormalAlpha =this->m_myDialog.myARButtons[i]->fTargNormalAlpha;

		}



	}

	//---------------------------------------------------------------------
	void NDMenuDialog::animateDialog(float fElapsedTime)
	{
		float aniTime =fElapsedTime;
		if (aniTime >0.3f)
			aniTime =0.3f;

		//bool bCheckState =true;
		if (NDVec2Length(&(this->m_myDialog.vTargetPosition -this->m_myDialog.vCurrentPosition)) >0.5f)
		{
			this->m_myDialog.vCurrentPosition +=(this->m_myDialog.vTargetPosition -this->m_myDialog.vCurrentPosition)*0.5f*aniTime*15.0f;
			//bCheckState =false;
		}else
			this->m_myDialog.vCurrentPosition =this->m_myDialog.vTargetPosition;

		if (fabs(this->m_myDialog.fTargWidth -this->m_myDialog.fCurrWidth)>0.5f)
		{
			this->m_myDialog.fCurrWidth +=(this->m_myDialog.fTargWidth -this->m_myDialog.fCurrWidth)*0.5f*aniTime*15.0f;
			//bCheckState =false;
		}else
			this->m_myDialog.fCurrWidth =this->m_myDialog.fTargWidth;

		//if (bCheckState)
		//{
		//	if (this->m_myDialog.nState ==1)
		//		this->m_myDialog.nState =2;

		//	if (this->m_myDialog.nState ==3)
		//		this->m_myDialog.nState =0;
		//}


	}


	//---------------------------------------------------------------------
	NDMenuDialog::NDMenuDialog( NDArk* pArk ) :NDBaseObject(99)
	{
		this->setArk( pArk );
		m_nCurrMouseOverButton =-1;
	}

	//---------------------------------------------------------------------
	void NDMenuDialog::release()
	{
		for (int i=0;i<m_myDialog.myARButtons.GetSize();++i)
		{
			delete m_myDialog.myARButtons[i];
		}

		for (int i=0;i<m_myDialog.myNormalButtons.GetSize();++i)
		{
			delete m_myDialog.myNormalButtons[i];
		}

		this->m_bReleased =true;
	}

	//---------------------------------------------------------------------
	void NDMenuDialog::initialize()
	{
		//init dialog
		m_myDialog.nBGTexID =NDTEXMGR->addTextureFromFile(L"../Resources/GUITextures/whitebg.tif", false);
		m_myDialog.nState =1; //normal mode
		m_myDialog.vCurrentPosition =NDVECTOR2(0.0f, 0.0f );
		m_myDialog.vTargetPosition  =NDVECTOR2(NDW32OS->getWindowWidth()-125.0f, 0.0f );
		m_myDialog.fCurrWidth =0.0f;
		m_myDialog.fTargWidth =125.0f;

		//init buttons
		//ar buttons
		sButtonInfo* pBI =new sButtonInfo;
		pBI->fCurrMouseOverAlpha =0.0f;
		pBI->fTargMouseOverAlpha =0.1f;
		pBI->fCurrNormalAlpha =0.0f;
		pBI->fTargNormalAlpha =1.0f;

		pBI->nFontID =NDGETUID();
		pBI->rc.left =pBI->rc.top =0;
		pBI->rc.right =125;
		pBI->rc.bottom =25;
		NDFONT->addString(&(pBI->nFontID), L"NORMAL MODE", L"Calibri", &(pBI->rc), /*DT_VCENTER|*/DT_CENTER, FW_BOLD, 10 );

		pBI->nMouseOverTexID =0;
		pBI->nNormalTexID =0;
		pBI->nState =0;
		pBI->vCurrentPosition =NDVECTOR2(0.0f, 0.0f);
		pBI->vTargetPosition  =NDVECTOR2(0.0f, 0.0f);
		m_myDialog.myARButtons.Add( pBI );

		//normal buttons
		sButtonInfo* pBI2 =0;

		//B0
		pBI2 =new sButtonInfo;
		pBI2->fCurrMouseOverAlpha =0.0f;
		pBI2->fTargMouseOverAlpha =0.1f;
		pBI2->fCurrNormalAlpha =0.0f;
		pBI2->fTargNormalAlpha =1.0f;

		pBI2->nFontID =NDGETUID();
		pBI2->rc.left =pBI2->rc.top =0;
		pBI2->rc.right =125;
		pBI2->rc.bottom =25;
		NDFONT->addString(&(pBI2->nFontID), L"OPEN FILE", L"Calibri", &(pBI2->rc), /*DT_VCENTER|*/DT_CENTER, FW_BOLD, 10 );

		pBI2->nMouseOverTexID =0;
		pBI2->nNormalTexID =0;
		pBI2->nState =0;
		pBI2->vCurrentPosition =NDVECTOR2(0.0f, 0.0f);
		pBI2->vTargetPosition  =NDVECTOR2(0.0f, 0.0f);
		m_myDialog.myNormalButtons.Add( pBI2 );

		////B1
		//pBI2 =new sButtonInfo;
		//pBI2->fCurrMouseOverAlpha =0.0f;
		//pBI2->fTargMouseOverAlpha =0.1f;
		//pBI2->fCurrNormalAlpha =0.0f;
		//pBI2->fTargNormalAlpha =1.0f;

		//pBI2->nFontID =NDGETUID();
		//pBI2->rc.left =pBI2->rc.top =0;
		//pBI2->rc.right =125;
		//pBI2->rc.bottom =25;
		//NDFONT->addString(&(pBI2->nFontID), L"SAVE AS", L"Calibri", &(pBI2->rc), /*DT_VCENTER|*/DT_CENTER, FW_BOLD, 10 );

		//pBI2->nMouseOverTexID =0;
		//pBI2->nNormalTexID =0;
		//pBI2->nState =0;
		//pBI2->vCurrentPosition =NDVECTOR2(0.0f, 0.0f);
		//pBI2->vTargetPosition  =NDVECTOR2(0.0f, 0.0f);
		//m_myDialog.myNormalButtons.Add( pBI2 );

		//B2
		pBI2 =new sButtonInfo;
		pBI2->fCurrMouseOverAlpha =0.0f;
		pBI2->fTargMouseOverAlpha =0.1f;
		pBI2->fCurrNormalAlpha =0.0f;
		pBI2->fTargNormalAlpha =1.0f;

		pBI2->nFontID =NDGETUID();
		pBI2->rc.left =pBI2->rc.top =0;
		pBI2->rc.right =125;
		pBI2->rc.bottom =25;
		NDFONT->addString(&(pBI2->nFontID), L"BOUNDING BOX", L"Calibri", &(pBI2->rc), /*DT_VCENTER|*/DT_CENTER, FW_BOLD, 10 );

		pBI2->nMouseOverTexID =0;
		pBI2->nNormalTexID =0;
		pBI2->nState =0;
		pBI2->vCurrentPosition =NDVECTOR2(0.0f, 0.0f);
		pBI2->vTargetPosition  =NDVECTOR2(0.0f, 0.0f);
		m_myDialog.myNormalButtons.Add( pBI2 );

		//B3
		pBI2 =new sButtonInfo;
		pBI2->fCurrMouseOverAlpha =0.0f;
		pBI2->fTargMouseOverAlpha =0.1f;
		pBI2->fCurrNormalAlpha =0.0f;
		pBI2->fTargNormalAlpha =1.0f;

		pBI2->nFontID =NDGETUID();
		pBI2->rc.left =pBI2->rc.top =0;
		pBI2->rc.right =125;
		pBI2->rc.bottom =25;
		NDFONT->addString(&(pBI2->nFontID), L"USING CHC", L"Calibri", &(pBI2->rc), /*DT_VCENTER|*/DT_CENTER, FW_BOLD, 10 );

		pBI2->nMouseOverTexID =0;
		pBI2->nNormalTexID =0;
		pBI2->nState =0;
		pBI2->vCurrentPosition =NDVECTOR2(0.0f, 0.0f);
		pBI2->vTargetPosition  =NDVECTOR2(0.0f, 0.0f);
		m_myDialog.myNormalButtons.Add( pBI2 );

		//B2
		pBI2 =new sButtonInfo;
		pBI2->fCurrMouseOverAlpha =0.0f;
		pBI2->fTargMouseOverAlpha =0.1f;
		pBI2->fCurrNormalAlpha =0.0f;
		pBI2->fTargNormalAlpha =1.0f;

		pBI2->nFontID =NDGETUID();
		pBI2->rc.left =pBI2->rc.top =0;
		pBI2->rc.right =125;
		pBI2->rc.bottom =25;
		NDFONT->addString(&(pBI2->nFontID), L"AR MODE", L"Calibri", &(pBI2->rc), /*DT_VCENTER|*/DT_CENTER, FW_BOLD, 10 );

		pBI2->nMouseOverTexID =0;
		pBI2->nNormalTexID =0;
		pBI2->nState =0;
		pBI2->vCurrentPosition =NDVECTOR2(0.0f, 0.0f);
		pBI2->vTargetPosition  =NDVECTOR2(0.0f, 0.0f);
		m_myDialog.myNormalButtons.Add( pBI2 );

		////B3
		//pBI2 =new sButtonInfo;
		//pBI2->fCurrMouseOverAlpha =0.0f;
		//pBI2->fTargMouseOverAlpha =0.1f;
		//pBI2->fCurrNormalAlpha =0.0f;
		//pBI2->fTargNormalAlpha =1.0f;

		//pBI2->nFontID =NDGETUID();
		//pBI2->rc.left =pBI2->rc.top =0;
		//pBI2->rc.right =125;
		//pBI2->rc.bottom =25;
		//NDFONT->addString(&(pBI2->nFontID), L"HELP", L"Calibri", &(pBI2->rc), /*DT_VCENTER|*/DT_CENTER, FW_BOLD, 10 );

		//pBI2->nMouseOverTexID =0;
		//pBI2->nNormalTexID =0;
		//pBI2->nState =0;
		//pBI2->vCurrentPosition =NDVECTOR2(0.0f, 0.0f);
		//pBI2->vTargetPosition  =NDVECTOR2(0.0f, 0.0f);
		//m_myDialog.myNormalButtons.Add( pBI2 );

	}

	//---------------------------------------------------------------------
	void NDMenuDialog::render( float fElapsedTime )
	{
		this->animateDialog( fElapsedTime );
		this->animateButton( fElapsedTime );

		if (this->m_myDialog.nState ==0)
		{
			//render dialog
			glColor4f(0.0f, 0.25f, 0.5f, 0.9f);
			NDTEXMGR->drawTexture( 
				this->m_myDialog.nBGTexID, 
				0.0f, 
				this->m_myDialog.vCurrentPosition.y, 
				this->m_nCurrWindowHeight, 
				this->m_myDialog.vCurrentPosition.x
				);
		}

		if (this->m_myDialog.nState ==1)
		{
			//render dialog
			glColor4f(0.0f, 0.25f, 0.5f, 0.9f);
			NDTEXMGR->drawTexture( 
				this->m_myDialog.nBGTexID, 
				this->m_myDialog.vCurrentPosition.x, 
				this->m_myDialog.vCurrentPosition.y, 
				this->m_nCurrWindowHeight, 
				fabs(this->m_nCurrWindowWidth-this->m_myDialog.vCurrentPosition.x)
				);

			//normal button
			INT nButtonPosiX =this->m_myDialog.vCurrentPosition.x+0.0f;
			INT nButtonPosiY =this->m_myDialog.vCurrentPosition.y+25.0f;
			for (int i=0;i<this->m_myDialog.myNormalButtons.GetSize();++i)
			{
				NDFONT->SetupStringColor( this->m_myDialog.myNormalButtons[i]->nFontID, NDCOLOR(1.0f, 1.0f, 1.0f, this->m_myDialog.myNormalButtons[i]->fCurrNormalAlpha ) );
				NDFONT->drawString(
					(float)nButtonPosiX,
					(float)nButtonPosiY+( (25.0f -NDFONT->GetStringHeight( this->m_myDialog.myNormalButtons[i]->nFontID ))*0.5f ),
					this->m_myDialog.myNormalButtons[i]->nFontID,
					fElapsedTime);

				//mouse over
				glColor4f( 1.0f, 0.7f, 0.0f, this->m_myDialog.myNormalButtons[i]->fCurrMouseOverAlpha );
				NDTEXMGR->drawTexture( this->m_myDialog.nBGTexID, nButtonPosiX, nButtonPosiY, 25.0f, 125.0f ); 
				NDFONT->SetupStringColor( this->m_myDialog.myNormalButtons[i]->nFontID, NDCOLOR(0.0f, 0.25f, 0.5f, this->m_myDialog.myNormalButtons[i]->fCurrMouseOverAlpha ) );
				NDFONT->drawString( 
					(float)nButtonPosiX,
					(float)nButtonPosiY+( (25.0f -NDFONT->GetStringHeight( this->m_myDialog.myNormalButtons[i]->nFontID ))*0.5f ),
					this->m_myDialog.myNormalButtons[i]->nFontID,
					fElapsedTime);

				nButtonPosiY +=25.0f;
			}

		}

		if (this->m_myDialog.nState ==2)
		{
			
			//render dialog
			glColor4f(0.0f, 0.25f, 0.5f, 0.9f);
			NDTEXMGR->drawTexture( 
				this->m_myDialog.nBGTexID, 
				0.0f, 
				this->m_myDialog.vCurrentPosition.y, 
				this->m_nCurrWindowHeight, 
				this->m_myDialog.vCurrentPosition.x+125.0f
				);

			//ar button
			INT nButtonPosiX =this->m_myDialog.vCurrentPosition.x+0.0f;
			INT nButtonPosiY =this->m_myDialog.vCurrentPosition.y+25.0f;
			for (int i=0;i<this->m_myDialog.myARButtons.GetSize();++i)
			{
				NDFONT->SetupStringColor( this->m_myDialog.myARButtons[i]->nFontID, NDCOLOR(1.0f, 1.0f, 1.0f, this->m_myDialog.myARButtons[i]->fCurrNormalAlpha ) );
				NDFONT->drawString(
					(float)nButtonPosiX,
					(float)nButtonPosiY+( (25.0f -NDFONT->GetStringHeight( this->m_myDialog.myARButtons[i]->nFontID ))*0.5f ),
					this->m_myDialog.myARButtons[i]->nFontID,
					fElapsedTime);

				//mouse over
				glColor4f( 1.0f, 0.7f, 0.0f, this->m_myDialog.myARButtons[i]->fCurrMouseOverAlpha );
				NDTEXMGR->drawTexture( this->m_myDialog.nBGTexID, nButtonPosiX, nButtonPosiY, 25.0f, 125.0f ); 
				NDFONT->SetupStringColor( this->m_myDialog.myARButtons[i]->nFontID, NDCOLOR(0.0f, 0.25f, 0.5f, this->m_myDialog.myARButtons[i]->fCurrMouseOverAlpha ) );
				NDFONT->drawString( 
					(float)nButtonPosiX,
					(float)nButtonPosiY+( (25.0f -NDFONT->GetStringHeight( this->m_myDialog.myARButtons[i]->nFontID ))*0.5f ),
					this->m_myDialog.myARButtons[i]->nFontID,
					fElapsedTime);

				nButtonPosiY +=25.0f;
			}

		}



	}

	//---------------------------------------------------------------------
	void NDMenuDialog::mouseMove(UINT nX, UINT nY)
	{
		POINT pt;
		pt.x =nX;
		pt.y =nY;

		m_nCurrMouseOverButton =-1;

		//NORMAL
		if (this->m_myDialog.nState ==1)
		{
			for (int i=0;i<this->m_myDialog.myNormalButtons.GetSize();++i)
			{
				RECT rc =this->m_myDialog.myNormalButtons[i]->rc;
				rc.top +=25;
				rc.top +=(i*25);

				rc.bottom +=25;
				rc.bottom +=(i*25);

				rc.left +=this->m_myDialog.vCurrentPosition.x;
				rc.right +=this->m_myDialog.vCurrentPosition.x;

				if (PtInRect(&rc, pt))
				{
					//mouse over
					this->m_myDialog.myNormalButtons[i]->fTargMouseOverAlpha =1.0f;
					this->m_myDialog.myNormalButtons[i]->fTargNormalAlpha =0.1f;

					m_nCurrMouseOverButton =i;

				}else
				{
					//normal button
					this->m_myDialog.myNormalButtons[i]->fTargMouseOverAlpha =0.1f;
					this->m_myDialog.myNormalButtons[i]->fTargNormalAlpha =1.0f;

				}

			}
		}

		//ar
		if (this->m_myDialog.nState ==2)
		{
			for (int i=0;i<this->m_myDialog.myARButtons.GetSize();++i)
			{
				RECT rc =this->m_myDialog.myARButtons[i]->rc;
				rc.top +=25;
				rc.top +=(i*25);

				rc.bottom +=25;
				rc.bottom +=(i*25);

				rc.left +=this->m_myDialog.vCurrentPosition.x;
				rc.right +=this->m_myDialog.vCurrentPosition.x;

				if (PtInRect(&rc, pt))
				{
					//mouse over
					this->m_myDialog.myARButtons[i]->fTargMouseOverAlpha =1.0f;
					this->m_myDialog.myARButtons[i]->fTargNormalAlpha =0.1f;

					m_nCurrMouseOverButton =i;

				}else
				{
					//normal button
					this->m_myDialog.myARButtons[i]->fTargMouseOverAlpha =0.1f;
					this->m_myDialog.myARButtons[i]->fTargNormalAlpha =1.0f;

				}

			}		
		}


	}

	//---------------------------------------------------------------------
	void NDMenuDialog::mouseLButtonDown()
	{
		m_nMouseDownButtonID =this->m_nCurrMouseOverButton;
	}

	//---------------------------------------------------------------------
	void NDMenuDialog::mouseLButtonUp()
	{
		if ((this->m_nCurrMouseOverButton !=this->m_nMouseDownButtonID) || (this->m_nMouseDownButtonID ==-1))
			return;
		else
			this->m_pCBButtonEvent( this->m_nCurrMouseOverButton );

	}

	//---------------------------------------------------------------------
	void NDMenuDialog::mouseRButtonDown()
	{
	}

	//---------------------------------------------------------------------
	void NDMenuDialog::mouseRButtonUp()
	{
	}

	//---------------------------------------------------------------------
	void NDMenuDialog::setWindowSz( UINT nW, UINT nH )
	{
		this->m_nCurrWindowHeight =nH;
		this->m_nCurrWindowWidth  =nW;

		if (this->m_myDialog.nState ==1)
		{
			this->m_myDialog.vTargetPosition.x =nW -this->m_myDialog.fTargWidth;
			//this->m_myDialog.vTargetPosition.y =0;
		}

		

	}

	//---------------------------------------------------------------------
	void NDMenuDialog::setCurrMode(int nMode )
	{

		switch(nMode)
		{
		case 0:
			{
				m_myDialog.vTargetPosition  =NDVECTOR2(0.0f, 0.0f );
				m_myDialog.fTargWidth =0.0f;
			}
			break;

		case 1:
			{
				//m_myDialog.nState =1; //normal mode
				//m_myDialog.vCurrentPosition =NDVECTOR2(0.0f, 0.0f );
				m_myDialog.vTargetPosition  =NDVECTOR2(NDW32OS->getWindowWidth()-125.0f, 0.0f );
				//m_myDialog.fCurrWidth =0.0f;
				m_myDialog.fTargWidth =125.0f;
			}
			break;

		case 2:
			{
				m_myDialog.vTargetPosition  =NDVECTOR2( 0.0f, 0.0f );
				m_myDialog.fTargWidth		=125.0f;

			}
			break;

		}

		this->m_myDialog.nState =nMode;
	}

	//---------------------------------------------------------------------
	void NDMenuDialog::setCallbackFunc(PCALLBACKNDMENUDIALOGMODECHANGE pCBMODECHANGE, PCALLBACKNDMENUDIALOGBUTTONEVENT pCBBUTTONEVENT)
	{
		this->m_pCBModeChange =pCBMODECHANGE;
		this->m_pCBButtonEvent =pCBBUTTONEVENT;

	}
}